home *** CD-ROM | disk | FTP | other *** search
/ Mac Expert 1995 Winter / Mac Expert - Winter 95.iso / Les fichiers / Tidbits / Tidbits 01 à 100 / TidBITS#082_System_Heap.txt < prev    next >
Encoding:
Text File  |  1995-06-19  |  17.0 KB  |  370 lines  |  [TEXT/ttxt]

  1. TidBITS#82/System_Heap
  2. ======================
  3.  
  4.  Copyright 1990-1992 Adam & Tonya Engst. Non-profit, non-commercial
  5.    publications may reprint articles if full credit is given. Other
  6.    publications please contact us. We do not guarantee the accuracy
  7.    of articles. Publication, product, and company names may be
  8.    registered trademarks of their companies. Disk subscriptions and
  9.    back issues are available.
  10.  
  11.  For more information send electronic mail to info@tidbits.uucp or
  12.  Internet: ace@tidbits.uucp -- CIS: 72511,306 -- AOL: Adam Engst
  13.  TidBITS -- 9301 Avondale Rd. NE Q1096 -- Redmond, WA 98052 USA
  14.  -----------------------------------------------------------------
  15.  
  16. Topics:
  17.     INIT Introduction
  18.     Diving In
  19.     Onto INITs!
  20.     The Killer INIT
  21.     Little Known Facts
  22.     Summing It Up
  23.  
  24.  
  25. INIT Introduction
  26. -----------------
  27.  
  28. INITs, the System Heap, and You
  29.  
  30.   by Andrew Welch
  31.  
  32.   This document came about because of the System Heap/INIT paranoia
  33.   I've seen lately. It represents the total sum of the knowledge I
  34.   have accumulated from writing system level software
  35.   (INITs/cdevs/System Extensions) for three years and is accurate
  36.   based on my experience and the experiences of many other Macintosh
  37.   programmers.
  38.  
  39.   It will help you make rational decisions about resolving INIT
  40.   conflicts, dispel some common myths, teach you a thing or two, and
  41.   explain what really happens with all this System Heap stuff.
  42.  
  43.   Neophytes, forgive me if I confuse you with the programmer-speak
  44.   necessary to explain this topic. Programmers, forgive me if I
  45.   generalize issues for the sake of clarity.
  46.  
  47.   INITs, cdevs, Control Panels, System Extensions - these buzz-words
  48.   stand for basically the same critters: low-level programs that
  49.   load automatically when the system starts up. These are the
  50.   programs that you toss into your System Folder. From now on, I
  51.   will refer to them all as INITs, though there are some subtle
  52.   differences between them that aren't important in this discussion.
  53.  
  54.   INITs are notorious for causing system crashes, conflicts, and
  55.   general system weirdness. When technical support people hear that
  56.   an INIT is causing problems, you'll inevitably hear something to
  57.   the effect of "Have you tried expanding your System Heap?"  What
  58.   do they mean when they say this, and is it the ultimate solution?
  59.  
  60.   You may learn more about the way your computer works than you want
  61.   to, but if you persevere you won't regret it. Knowledge is power.
  62.   Hang on tight. But first, a little background...
  63.  
  64.  
  65. Diving In
  66. ---------
  67.   For starters, your Macintosh has a fixed amount of memory
  68.   installed in it, which acts like desk space for things you are
  69.   currently working on. In real life, when you want to work on
  70.   something you might take it out of your filing cabinet and put it
  71.   on your desk where you can work on it effectively. This is similar
  72.   to what a computer does: all of your programs and documents are
  73.   stored on your hard disk, and when you want to work on them they
  74.   are read in from disk and placed in memory.
  75.  
  76.   Life being what it is, you can't have everything. You only have a
  77.   limited amount of memory that must be rationed out to the programs
  78.   that you run. In fact, each program you run grabs a fixed chunk of
  79.   memory when it loads, and it doesn't let go of the memory until
  80.   you quit the program. This is the application's world to do with
  81.   as it pleases. This is also known in technoweenie terms as a heap.
  82.  
  83.   Each application is said to have its own memory heap. You can set
  84.   the size of this heap (how much memory the application will use,
  85.   or how much of your desk space you're giving up to use the
  86.   program). You can do this for any program by choosing "Get Info"
  87.   for it in the Finder and entering how much memory you want it to
  88.   use. That assumes you are running under System 7 or System 6's
  89.   MultiFinder. Under older systems (sometimes called UniFinder) the
  90.   programs that you run use all of your Macintosh's memory, so you
  91.   can only run one program at a time.
  92.  
  93.   Well actually I just lied. Sorry about that, I realize I hardly
  94.   know you and I've lied to you already. Your Macintosh has another
  95.   heap that is always around (even if you aren't running any
  96.   programs) called the System Heap. This is important. The System
  97.   Heap is a chunk of memory that the system software (the nitty
  98.   gritty, low level stuff) uses for its own purposes. Things with
  99.   nasty names like device drivers, ROM patch code, etc. dwell there.
  100.   It is a murky, convoluted place of which only Apple knows every
  101.   nook and cranny, but a very important one nevertheless.
  102.  
  103.   So, a portion of your computer's memory (or RAM) is always
  104.   occupied by the System Heap, an area of memory designated for the
  105.   low-level system software from Apple and other nasties. The rest
  106.   of your computer's memory is rationed off in chunks to the
  107.   applications that you run on a first-come, first-serve basis.
  108.   Normally, we developers stay in our own little application heaps,
  109.   insulated from the complexities of system-level software. There
  110.   are times however that the only possible way to write the programs
  111.   we dream of is to delve into the world of system software and the
  112.   System Heap. Yes friends, we are talking about INITs (you knew I'd
  113.   get around to it some day, right?).
  114.  
  115.  
  116. Onto INITs!
  117. -----------
  118.   If a program wants to achieve some kind of a global effects (like
  119.   QuicKeys allowing you to define macros that work in any program),
  120.   it has to find out how to graft itself into your system and keep a
  121.   portion of memory for itself that will stay around even when a
  122.   program quits (remember folks, the memory a program allocates for
  123.   itself is freed up again when it quits).
  124.  
  125.   We are talking sophisticated stuff here. Writing system software
  126.   is extremely tricky stuff, and even for Apple who knows every
  127.   little subtle nuance in the Macintosh, it is a tough thing to do
  128.   right. That is why System 7 took so long and why Apple should be
  129.   praised for System 7 - it works well, which is nothing short of a
  130.   miracle. But I digress.
  131.  
  132.   You get the idea, writing system software is not for the faint of
  133.   heart. In any case, INITs take a portion of System Heap memory for
  134.   themselves when the computer starts up. It is also then that they
  135.   "hook" themselves into the system software. When your computer
  136.   first starts up, the System Heap is a certain fixed size. However,
  137.   during the start up process, the System Heap grows automagically
  138.   to accommodate an INIT that needs more memory for itself.
  139.  
  140.   For technoweenies, the mechanism that loads INITs at start up time
  141.   and expands the System Heap as needed is called INIT31. There is a
  142.   well known mechanism that any INIT writer worth his salt takes
  143.   advantage of: 'sysz', or System Zone Expansion. This is a little
  144.   parcel that all INIT writers should include in their products (it
  145.   takes all of two minutes to put it in), because it tells the
  146.   INIT31 mechanism how much memory the INIT needs to load. INIT31
  147.   grows the System Heap so that there is at least this much memory
  148.   free, and then loads and runs the INIT. In this way, any well-
  149.   written INIT will tell INIT31 how much memory it needs, get it,
  150.   and be happy. All is well in INIT-land.
  151.  
  152.   In a perfect world, our story would end here. But as I'm sure you
  153.   suspect, there are a few twists. INITs have the power to affect
  154.   the operation of your entire computer, and unfortunately they can
  155.   also crash the whole thing too. INITs can run into several
  156.   different snags regarding the System Heap, and I will go into them
  157.   one by one so that you can understand what goes wrong and why.
  158.  
  159.   One thing we should get straight now - if your machine crashes
  160.   because of an INIT, a programmer out there somewhere is at fault.
  161.   Count on it. There are well established rules for writing INITs,
  162.   so the real problem is that not all programmers understand the
  163.   rules. Information on how to write INITs properly is also hard to
  164.   come by, and programmers are only human - we're doing our best but
  165.   we make mistakes.
  166.  
  167.  
  168. The Killer INIT
  169. ---------------
  170.   When an INIT causes a problem, you'll hear ten people shout in
  171.   unison, "Have you increased the size of your System Heap?"  There
  172.   are utilities out there that let you manually make the System Heap
  173.   bigger in an effort to fix crashes due to INITs. However this
  174.   isn't much of a solution. In some cases, increasing the size of
  175.   your System Heap by 20K or so can be beneficial, because it gives
  176.   the System Heap a little breathing room. If you still experience
  177.   problems, increasing the System Heap more probably won't help you.
  178.   It is kind of like this:
  179.  
  180.   You are in a room with a sadistic murder wielding a nasty looking
  181.   knife, who definitely doesn't mean well for you. The bigger the
  182.   room, the better chance you have to escape from him. Heck, if he
  183.   is out of shape and the room is big enough, he may never catch
  184.   you. But one day when you least expect it...
  185.  
  186.   The same goes for the System Heap size method of "curing" crashes.
  187.   Here are the common causes of problems INITs can run into, and
  188.   what you can do about them:
  189.  
  190.  
  191. PROBLEM #1
  192.   Many INITs are simply badly written. No amount of fudging will
  193.   help you avoid crashes from a badly written INIT, or two INITs
  194.   that don't get along together. True, increasing the size of your
  195.   System Heap may delay the inevitable. For instance, some INITs
  196.   don't even bother to check if they actually have enough memory to
  197.   do what they want, and increasing your System Heap a little (20K
  198.   or so) should help this. But if an INIT is badly written, there
  199.   may be nothing you can do about it. Increasing your System Heap
  200.   memory to fix a blatant programming error is just a waste of
  201.   memory.
  202.  
  203.  
  204. PROBLEM #2
  205.   Many INITs don't bother to have 'sysz's, thus the INIT31 mechanism
  206.   has no way of knowing how much memory to give to the INIT. In this
  207.   case, increasing the size of your System Heap DOES INDEED cure!
  208.   However the 'sysz' method has been documented by Apple for years,
  209.   and I'd be suspicious of an INIT that doesn't take advantage of
  210.   'sysz' anyway.
  211.  
  212.  
  213. PROBLEM #3
  214.   To understand this problem, I'll need to tell you a bit more about
  215.   heaps. Heaps are called heaps for good reason. Imagine a big
  216.   laundry basket into which you throw all your clothes. Heaps work
  217.   in a similar manner. Fortunately for programmers, a maid called
  218.   Memory Manager takes care of this mess. I call her Martha (just
  219.   one of those things that keeps you sane when dealing with
  220.   something that is NEVER wrong).
  221.  
  222.   When you want to put something in a heap, you ask Martha for the
  223.   space for it, and she gives it to you if she can. Like all good
  224.   maids, Martha knows that there is less space in a messy heap. So
  225.   if she can't give you what you ask for immediately, she takes
  226.   everything in the heap and cleans it up a bit to free up more
  227.   room. This is a nice thing, but we all know the problem of coming
  228.   into your room after someone else cleaned it - you can never find
  229.   anything! Many INITs have this problem as well, and it is
  230.   absolutely their fault, not Martha's. She can tell you where
  231.   anything is, but some INITs don't bother to ask, they simply
  232.   assume they know the location of their little chunk of memory and
  233.   **crash**.
  234.  
  235.   There is one other twist to this situation as well. If Martha
  236.   cleans everything up, and there still isn't enough room to satisfy
  237.   a request for memory, she starts throwing things out. But it again
  238.   isn't her fault, these things are specifically marked "Throw me
  239.   out if you need the space." If an INIT doesn't take this into
  240.   account, **crash**.
  241.  
  242.   Since the System Heap is shared by system software and all of your
  243.   INITs, it is particularly active. However the whole scheme can and
  244.   should work. Quite well in fact.
  245.  
  246.   In this situation, expanding your System Heap can delay crashes,
  247.   because if Martha never has to move things or throw things out,
  248.   badly written software will work most of the time. Realize though
  249.   that you are sacrificing your precious memory to correct a defect
  250.   in the software for which you paid good money.
  251.  
  252.  
  253. PROBLEM #4
  254.   This is a tricky one that many programmers don't understand fully.
  255.   Earlier I described the 'sysz' method for telling INIT31 how much
  256.   memory an INIT needs to loads. It works as advertised, but there
  257.   is a rub.
  258.  
  259.   The System Heap is shared memory, it is one big basket into which
  260.   many hens put their eggs. Lets say we have an INIT called
  261.   "Longcut" that has a 'sysz' specifying that it needs 50K of
  262.   memory. INIT31 will free up 50K of memory, load the hypothetical
  263.   "Longcut" INIT, and all should be well.
  264.  
  265.   However, let's say "Longcut" doesn't use all 50K of the memory it
  266.   asks for at start up time. Let's say it uses 10K of memory at
  267.   start up time to install itself, reserving the rest for later on
  268.   when it actually works its magic. This is actually a very common
  269.   situation. Logic would tell you that there should be 40K of memory
  270.   left over that "Longcut" can count on being there, right? WRONG!
  271.  
  272.   As I said before, the System Heap is shared memory. If an INIT
  273.   asks for more memory than it actually uses at start up time,
  274.   whatever it doesn't use isn't reserved for it automatically;
  275.   instead it is thrown "back in the pot," and can be used up by
  276.   anyone. So later on when "Longcut" thinks it has room to spare, it
  277.   actually might not and then, **crash**.
  278.  
  279.   Of course, some programmers know ways to properly program around
  280.   this situation, but this particular situation is not well known.
  281.   In this case, increasing System Heap memory can be beneficial as
  282.   well, but only in a limited sense. Only the programmers know how
  283.   much memory their INITs really need, and they should be the ones
  284.   to fix the problem.
  285.  
  286.  
  287. Little Known Facts
  288. ------------------
  289.   Many people do not realize this, but with all versions of
  290.   MultiFinder, the System Heap can actually grow even _after_ start
  291.   up time, easing INIT memory conflicts. If an INIT asks for more
  292.   memory than is available, the System Heap will automagically get
  293.   bigger. However there are two bad things about this: 1) the System
  294.   Heap will grow, but never shrink again and 2) Several well
  295.   respected Macintosh programmers have stated that they've seen
  296.   cases where MultiFinder CANNOT expand the System Heap
  297.   automatically, and die in the process. This is apparently
  298.   extremely rare however.
  299.  
  300.   This makes claims of "you've got to increase your System Heap"
  301.   even less powerful, because the system will increase it for you if
  302.   need be.
  303.  
  304.   System 7 changes the game entirely, the System Heap can grow and
  305.   shrink at will to accommodate requests for memory, eliminating
  306.   many INIT memory problems entirely. However System 7 threw INIT
  307.   writers several other curves, so you'll most likely need to
  308.   upgrade your favorite INITs for use under System 7.0. You
  309.   shouldn't have to increase your System Heap manually for System
  310.   7.0, but you never know... an extra 20K or so never hurt anyone.
  311.  
  312.  
  313. Summing It Up
  314. -------------
  315.   Properly written INITs shouldn't exhibit any of the above stated
  316.   problems. But if your Macintosh needs a little tweaking to get it
  317.   working smoothly again, there is nothing wrong to giving a little
  318.   more memory to the System Heap. In no case should you ever have to
  319.   allocate more than 50K of extra memory to your System Heap. After
  320.   a point, no amount of fudging will save you. Try to identify the
  321.   culprit INIT that causes problems, and trash it. Or better yet,
  322.   make sure you have the latest version of every INIT you use.
  323.   Realize that INIT crashes/conflicts _do_not_ stem from lack of
  324.   System Heap memory, but from improperly written software. Giving
  325.   the System Heap extra memory may circumvent some problems, but it
  326.   is not the source of them.
  327.  
  328.   When you choose "About the Finder" from the Apple Menu, there
  329.   should be a little bit of white space (free memory) in the
  330.   "System" memory bar. 5-10% or so should be fine.
  331.  
  332.   With that in mind, here are a few recommendations. INITs are
  333.   particularly sensitive pieces of code, so expect them to be
  334.   upgraded more frequently then other software products, and keep up
  335.   with the current versions. Your Macintosh will be more stable if
  336.   you use the current versions of the INITs you use, and if you use
  337.   INITs only from well known and established vendors who support
  338.   their products. Taking these precautions will result in a far more
  339.   stable Macintosh than any amount of System Heap tweaking ever
  340.   could.
  341.  
  342.   20K - 50K of extra System Heap space (free System Heap memory),
  343.   OK. That is a nice margin of error. More than that? Forget it,
  344.   tell them to fix it!
  345.  
  346.   If you are interested in increasing the size of your System Heap,
  347.   here are three free programs that manually adjust System Heap
  348.   size:
  349.  
  350.     Heap Fixer
  351.     BootMan
  352.     Heap Tool
  353.  
  354.   Contact the author at:
  355.  
  356.     Andrew Welch
  357.     Mark 3 Software
  358.     29 Grey Rocks Road
  359.     Wilton, CT  06897
  360.  
  361.   ...and if you include a disk and a self-addressed, stamped mailer,
  362.   I will send you the latest versions of my shareware programs!
  363.  
  364.  
  365. ..
  366.  
  367.  This text is encoded in the setext format. Please send email to
  368.  <info@tidbits.uucp> or contact us at one of the above addresses
  369.  to learn how to get more information on the setext format.
  370.